package com.jxin.mall.member.domain.service.impl;

import cn.hutool.captcha.LineCaptcha;
import com.jxin.mall.member.domain.service.ValidateCodeImageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

/**
 * 图形验证码服务实现类
 * @author  蔡佳新
 * @version 1.0
 * @since   jdk 1.8
 */
@Service("validateCodeImageService")
@Slf4j
public class ValidateCodeImageServiceImpl implements ValidateCodeImageService {
    /**验证码图片宽*/
    private static final int DEF_IMAGE_WIDTH = 200;
    /**验证码图片高*/
    private static final int DEF_IMAGE_HEIGHT = 100;

    /**
     * 在http响应流中加入验证码图片
     * @param  code 验证码
     * @param  response http响应
     * @author 蔡佳新
     */
    @Override
    public void buildDefLineCaptchaImg(String code, HttpServletResponse response) {
        LineCaptcha lineCaptcha = new LineCaptcha(DEF_IMAGE_WIDTH, DEF_IMAGE_HEIGHT);
        lineCaptcha.createImage(code);
        try (final ServletOutputStream outputStream = response.getOutputStream()){
            lineCaptcha.write(outputStream);
        } catch (Exception e){
            log.error(e.getMessage(), e);
        }
    }
}
